package com.centit.ideaInfo.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.framework.core.dao.CodeBook;
import com.centit.framework.jdbc.dao.BaseDaoImpl;
import com.centit.framework.jdbc.dao.DatabaseOptUtils;
import com.centit.ideaInfo.po.IdeaInfo;
import com.centit.support.database.utils.PageDesc;
import com.centit.support.database.utils.QueryAndNamedParams;
import com.centit.support.database.utils.QueryUtils;
@Repository
public class IdeaInfoDao extends BaseDaoImpl<IdeaInfo,java.lang.String>{
	public static final Log log = LogFactory.getLog(IdeaInfoDao.class);
	
	@Override
	public Map<String, String> getFilterField() {
		if( filterField == null){
			filterField = new HashMap<String, String>();

			filterField.put("recId" , CodeBook.EQUAL_HQL_ID);

			filterField.put("title" , CodeBook.EQUAL_HQL_ID);

			filterField.put("userCode" , CodeBook.EQUAL_HQL_ID);

			filterField.put("userName" , CodeBook.EQUAL_HQL_ID);

			filterField.put("unitCode" , CodeBook.EQUAL_HQL_ID);

			filterField.put("unitName" , CodeBook.EQUAL_HQL_ID);

			filterField.put("content" , CodeBook.EQUAL_HQL_ID);

			filterField.put("dataValid" , CodeBook.EQUAL_HQL_ID);
			
			filterField.put("flowRecId" , CodeBook.EQUAL_HQL_ID);
			
		}
		return filterField;
	} 
	
	public JSONArray listIdeaInfo(Map<String, Object> searchColumn, PageDesc pageDesc){
		String sql="select i.*,f.NODE_NAME from T_IDEA_INFO i left join T_FLOW_NODE f on i.REC_ID=f.IDEA_REC_ID "
				+ " where 1=1 "
				 + "[ :flowRecId| and i.air_rec_id = :flowRecId] order by update_time asc";
		 QueryAndNamedParams queryAndNamedParams = QueryUtils.translateQuery(sql, searchColumn);
	        JSONArray json = DatabaseOptUtils.listObjectsByNamedSqlAsJson(this,
	                queryAndNamedParams.getQuery(), queryAndNamedParams.getParams(), pageDesc);
	        return json;
	}
	
	public void deleteApplyIdea(String flowRecId) {
		String str="update T_IDEA_INFO set data_valid='F' where FLOW_REC_ID=?";
		DatabaseOptUtils.doExecuteSql(this, str, new Object[] {flowRecId});
	}
	
	public void updateLastIdeaInfo(String flowRecId, String userCode, String content) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sql="update T_IDEA_INFO set update_time= to_date(?,'yyyy-MM-dd HH24:mi:ss'),content=?,idea_code='已办' where FLOW_REC_ID=? and user_code=? and update_time is null and data_valid='T' ";
		DatabaseOptUtils.doExecuteSql(this, sql, new Object[] {sf.format(new Date()),content,flowRecId,userCode});
	}
	
	
	public JSONObject getLastIdeaInfo(String flowRecId) {
		String sql="SELECT * FROM (select * from T_IDEA_INFO where FLOW_REC_ID=? order by UPDATE_TIME ) where rownum=1";
		return DatabaseOptUtils.getObjectBySqlAsJson(this, sql,new Object[]{flowRecId});
		
		
	}
}

